<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**产品类型关联表model */
class ResourceTotalTypeIntention extends BaseModel
{

    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    public $table = 'resource_total_type_intention';


    /**
     * 添加作品类型关联信息
     * @param resource_id 作品id
     * @param change_id_value 类型名称  大类小类  | 链接  多个类之间  逗号平截面
     */
    public function resourceTypeIntentionModify($resource_id, $change_id_value, $type = 'add')
    {
        $change_id_value = empty($change_id_value) ? [] : explode(',', $change_id_value);
        $change_id_value = array_filter(array_unique($change_id_value));
        if ($type == 'add') {
            $change_id_value_del = [];
        } else {
            $change_id_value_del = $this->select('id')->where('resource_id', $resource_id)->get()->toArray();
            $change_id_value_del = array_column($change_id_value_del, 'id', 'id');
        }
        // dump($change_id_value_del);die;
        foreach ($change_id_value as $key => $val) {
            $type_id = explode('|', $val);
            $type_id = array_filter($type_id);
            if (count($type_id) != 2) {
                continue;
            }
            $type_info = $this->where('resource_id', $resource_id)->where('big_type_id', $type_id[0])->where('type_id', $type_id[1])->first();
            if ($type_info) {
                unset($change_id_value_del[$type_info['id']]);
            } else {
                $obj = new self();
                $obj->resource_id = $resource_id;
                $obj->big_type_id = $type_id[0];
                $obj->type_id = $type_id[1];
                $obj->save();
            }
        }
        if (!empty($change_id_value_del)) {
            $change_id_value_del = array_values($change_id_value_del);
            $this->whereIn('id', $change_id_value_del)->where('resource_id', $resource_id)->delete();
        }
    }



    /**
     * 获取类型的资源guid
     * @param type_id 类型id
     * @param resource_total_type_id 资源商id
     */
    public function getResourceTotalTypeResourceGuid($type_id, $resource_total_type_id = null)
    {
        $res = $this->where(function ($query) use ($type_id, $resource_total_type_id) {
            if ($type_id) {
                $type_id = !is_array($type_id) ? explode(',', $type_id) : $type_id;
                $query->whereIn('type_id', $type_id);
            }
            if ($resource_total_type_id) {
                $query->where('resource_total_type_id', $resource_total_type_id);
            }
        })->groupBy('resource_guid')->pluck('resource_guid')->toArray();
        return $res;
    }


    /**
     * 获取资源类型资源数据列表
     * @param limit int 分页大小
     * @param keywords int 检索id
     * @param resource_total_type_id 书单id
     */
    public function lists($search_data = [])
    {
        $resource_total_type_id = $search_data['resource_total_type_id'] ?? '';
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;
        $keywords = !empty($search_data['keywords']) ? $search_data['keywords'] : null;
        //获取书单数据
        $field = ['i.id', 'i.resource_guid', 'i.create_time', 'r.title', 'r.author', 'r.isbn', 'r.press', 'r.pre_time', 'r.img', 'r.intro'];
        $res = $this->from($this->getTable() . " as i")->select($field)
            ->join('resource_total as r', 'r.guid', '=', 'i.resource_guid')
            ->join('resource_business_name as n', 'n.id', '=', 'r.resource_business_id')
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('r.title', 'like', "%$keywords%");
                }
            })
            ->where('i.type_id', $resource_total_type_id)
            ->where('r.is_del', 1)
            ->where('n.is_del', 1)
            ->orderByDesc('i.id')
            ->paginate($limit)
            ->toArray();

        return $res;
    }

    /**
     * 添加资源类型作品
     * @param resource_total_type_id int 书单id
     * @param resource_guids int 资源作品的id 多个逗号拼接
     */
    public function addResourceTotalTypeResource($resource_total_type_id, $resource_guids)
    {
        $old_resource_guids = $this->where('type_id', $resource_total_type_id)->pluck('resource_guid')->toArray();
        $resource_guids = explode(',', $resource_guids);
        $resourceTotalModel = new ResourceTotal();
        $data = [];
        $i = 0;
        foreach ($resource_guids as $key => $val) {
            if (!in_array($val, $old_resource_guids)) {
                $data[$i]['type_id'] = $resource_total_type_id;
                $data[$i]['resource_guid'] = $val;
                $data[$i]['resource_business_id'] = $resourceTotalModel->where('guid', $val)->value('resource_business_id');
                $data[$i]['create_time'] = date('Y-m-d H:i:s');
                $data[$i]['change_time'] = date('Y-m-d H:i:s');
                $i++;
            }
        }
        if ($data) {
            $this->insert($data);
        }
        return true;
    }


    /**
     * 删除资源作品
     * @param resource_total_type_id int 书单id
     * @param resource_list_ids  数据id 多个逗号拼接
     */
    public function delResourceTotalTypeResource($resource_total_type_id, $resource_list_ids)
    {
        $resource_list_ids = explode(',', $resource_list_ids);
        return $this->where('type_id', $resource_total_type_id)->whereIn('id', $resource_list_ids)->delete();
    }


    /**
     * 获取系统类型作品
     * @param resource_total_type_id int 书单id
     */
    public function getBookHomeReadingListBookId($resource_total_type_id)
    {
        return $this->where('type_id', $resource_total_type_id)->pluck('resource_guid')->toArray();
    }
}
